package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Active;
import org.springblade.pm.entity.File;
import org.springblade.pm.service.IActiveService;
import org.springblade.pm.service.IFileService;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目管理活动
 */
@RestController
@RequestMapping("active")
@AllArgsConstructor
public class ActiveController {

	private IActiveService activeService;
	private IFileService fileService;
	private IUserClient userClient;

	@PostMapping("/insertActive")
	@ApiOperation(value = "进行活动新增")
	public R insertActive(@RequestParam("files") MultipartFile[] files, File file, Active active) {
		try {
			activeService.saveOrUpdate(active);
			file.setParentId(active.getId());
			file.setModuleType("active");
			fileService.saveBatch(files, file);
		} catch (Exception e) {
			e.printStackTrace();
			return R.status(false);
		}
		return R.status(true);
	}

	@GetMapping("/getActiveById")
	@ApiOperation(value = "根据id查询活动详情")
	public R getActiveById(Integer id) {
		Map<String, Object> rmap = new HashMap<>();
		try {
			Active active = activeService.getById(id);
			rmap.put("data",active);
		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, rmap, "exception:" + e.getMessage());
		}
		return R.data(rmap);
	}

	@GetMapping("/getFile")
	@ApiOperation(value = "根据活动id查询附件详情")
	public R<List<Map<String, Object>>> getFile(Integer id) {
		List<Map<String, Object>> list = null;
		try {
			list = activeService.getFile(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, list, "exception:" + e.getMessage());
		}
		return R.data(list);
	}


	@GetMapping("/getActive")
	@ApiOperation(value = "获取活动信息")
	public R<List<Active>> getActive() {
		List<Active> list = null;
		try {
			list = activeService.list();
		} catch (Exception e) {
			return R.data(200, list, "exception:" + e.getMessage());
		}
		return R.data(list);
	}

	/**
	 * 获取活动列表
	 */
	@PostMapping("/activeList")
	@ApiOperation(value = "活动列表")
	public R activeList(@RequestBody Map<String, Object> map, BladeUser user) {
		UserInfo userInfo = userClient.userInfo(Long.valueOf(user.getUserId())).getData();
		String projectId = map.get("projectId").toString();
		Integer page = Integer.valueOf(map.get("page").toString());
		Integer size = Integer.valueOf(map.get("pageSize").toString());
		String limit = getLimitStr(size, page);
		Map<String, Object> rmap = new HashMap<>();
		Map<String, Object> params = new HashMap<>();
		params.put("projectId", projectId);
		params.put("activeTitle",map.get("activeTitle").toString());
		Object activeTime = map.get("activeTime");
		if (activeTime!=null&&!"".equals(activeTime)) {
			String[] split = activeTime.toString().split(",");
			params.put("startTime", split[0]);
			params.put("endTime", split[1]);
		}
		try {
			List<Map<String, Object>> listMap = activeService.activeList(params);
			rmap.put("total", listMap.size());
			params.put("limit", limit);
			List<Map<String, Object>> list = activeService.activeList(params);
			rmap.put("data", list);
		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, rmap, "exception:" + e.getMessage());
		}
		return R.data(rmap);
	}

	/**
	 * 修改活动状态
	 * @return
	 */
	@GetMapping("/updateActiveStatus")
	@ApiOperation(value = "修改活动状态")
	public R updateActiveStatus(Active active){
		try {
			activeService.updateById(active);
			return R.data(1);
		}catch (Exception e) {
			return  R.data(0);
		}
	}

	/**
	 * 根据 页码、页大小 获取分页字符串 " LIMIT 1,10 "
	 */
	public static String getLimitStr(Integer size, Integer page) {
		String str = "";
		try {
			Integer num = (page - 1) * size;
			str = " LIMIT " + num + "," + size + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
}

